package com.wallet.crypto.trustapp.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wallet.crypto.trustapp.R;
import com.wallet.crypto.trustapp.createWallets.PasswordView;
import com.wallet.crypto.trustapp.ui.BaseActivity;
import com.wallet.crypto.trustapp.ui.ChoosePrivateKeyActivity;
import com.wallet.crypto.trustapp.util.Loger;
import com.wallet.crypto.trustapp.widget.CustomDialog;


/**
 * Created by lingjianzhong on 2018/5/10.
 */

public class ChangePINActivity extends BaseActivity {

    private ImageView mClose;
    private View mLine;
    private TextView mTitle;
    private TextView mPrompt;
    private PasswordView mPassword;
    private String mOriginalPassword;
    private String mNewPassword;
    private boolean isOriginal = true;
    private boolean isNew = true;
    private boolean isAmend = false;//是否修改
    private Intent mIntent;
    private int tag;
//    private boolean isOriginalError = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pin);
        initView();
    }

    private void initView() {
        mIntent = getIntent();
        isAmend = mIntent.getBooleanExtra("isAmend", false);
        tag = mIntent.getIntExtra("tag", 0);
        mLine = findViewById(R.id.top_line);
        mLine.setVisibility(View.INVISIBLE);
        mClose = findViewById(R.id.top_icon);
        mTitle = findViewById(R.id.pin_title);
        mPrompt = findViewById(R.id.pin_prompt);
        mPassword = findViewById(R.id.pin_password);
        if (!isAmend) {
            mTitle.setText(R.string.p0003);
            mPrompt.setText(R.string.p0002);
        }
        mPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 6) {
                    OnSetPassword(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mClose.setOnClickListener(this::OnCloseListener);
    }

    private void OnSetPassword(String s) {
        if (isAmend) {//修改密码
            if (isOriginal) {
                //进行原始密码的判断
                if (s.equals("123456")) {
                    mTitle.setText(R.string.p0005);
                    mPrompt.setText(R.string.p0007);
                    setColorNormal(mPrompt);
                    mOriginalPassword = s;
                    isOriginal = false;
                } else {
                    mPrompt.setText(R.string.p0004);
                    setColor(mPrompt);
                }
                textClear();
            } else {
                //输入新密码
                if (isNew) {
                    isNew = false;
                    mNewPassword = s;
                    mTitle.setText(R.string.p0006);
                    mPrompt.setVisibility(View.VISIBLE);
                    mPrompt.setText(R.string.p0007);
                    setColorNormal(mPrompt);
                    textClear();
                } else {
                    if (mNewPassword.equals(s)) {
                        Loger.e(" mNewPassword " + mNewPassword);
                        finish();
                    } else {
                        mPrompt.setText(R.string.p0004);
                        setColor(mPrompt);
                        textClear();
                    }
                }
            }
        } else {
            if (s.equals("123456")) {
                if (tag == 0) {
                    Intent intent = new Intent(this, ChoosePrivateKeyActivity.class);
                    startActivity(intent);
                    finish();
                } else if (tag == 1) {
                    setResult(1);
                    finish();
                }
            } else {
                mPrompt.setText(R.string.p0004);
                setColor(mPrompt);
                textClear();
            }
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        Loger.e(" onResume ");
        findViewById(R.id.pin_password).postDelayed(() -> {
            //显示软键盘
            openKeybord(mPassword, ChangePINActivity.this);
        }, 500);


    }

    @Override
    protected void onPause() {
        super.onPause();
        closeKeybord(mPassword, this);
    }

    //默认颜色
    private void setColorNormal(TextView view) {
        view.setTextColor(getResources().getColor(R.color.pin_normal));
    }

    private void setColor(TextView view) {
        view.setTextColor(getResources().getColor(R.color.pin_red));
    }

    private void OnCloseListener(View view) {
        if (tag == 1)
            setResult(0);
        finish();
    }

    public static void openKeybord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    public static void closeKeybord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }

    //延迟清除文本内容
    private void textClear() {
        new Thread(() -> {
            //sleep设置的是时长
            try {
                Thread.sleep(200);
                mPassword.getText().clear();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();
    }

    @Override
    public void finish() {
        super.finish();
        //关闭窗体动画显示
        this.overridePendingTransition(0, R.anim.activity_exit);
    }
}